package org.jeecg.modules.polaris.login.controller;

import java.util.Arrays;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.polaris.job.entity.Job;
import org.jeecg.modules.polaris.login.entity.GradLogin;
import org.jeecg.modules.polaris.login.service.IGradLoginService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.polaris.student.entity.Student;
import org.jeecg.modules.polaris.student.service.IStudentService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: grad_login
 * @Author: jeecg-boot
 * @Date:   2020-10-18
 * @Version: V1.0
 */
@Api(tags="grad_login")
@RestController
@RequestMapping("/polaris/gradLogin")
@Slf4j
public class GradLoginController extends JeecgController<GradLogin, IGradLoginService> {
	@Autowired
	private IGradLoginService gradLoginService;
	 @Autowired
	 private IStudentService studentService;
	/**
	 * 分页列表查询
	 *
	 * @param gradLogin
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "grad_login-分页列表查询")
	@ApiOperation(value="grad_login-分页列表查询", notes="grad_login-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GradLogin gradLogin,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GradLogin> queryWrapper = QueryGenerator.initQueryWrapper(gradLogin, req.getParameterMap());
		Page<GradLogin> page = new Page<GradLogin>(pageNo, pageSize);
		IPage<GradLogin> pageList = gradLoginService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param gradLogin
	 * @return
	 */
	@AutoLog(value = "grad_login-添加")
	@ApiOperation(value="grad_login-添加", notes="grad_login-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GradLogin gradLogin) {
		gradLoginService.save(gradLogin);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param gradLogin
	 * @return
	 */
	@AutoLog(value = "grad_login-编辑")
	@ApiOperation(value="grad_login-编辑", notes="grad_login-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GradLogin gradLogin) {
		gradLoginService.updateById(gradLogin);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "grad_login-通过id删除")
	@ApiOperation(value="grad_login-通过id删除", notes="grad_login-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		gradLoginService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "grad_login-批量删除")
	@ApiOperation(value="grad_login-批量删除", notes="grad_login-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gradLoginService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "grad_login-通过id查询")
	@ApiOperation(value="grad_login-通过id查询", notes="grad_login-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GradLogin gradLogin = gradLoginService.getById(id);
		if(gradLogin==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(gradLogin);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param gradLogin
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GradLogin gradLogin) {
        return super.exportXls(request, gradLogin, GradLogin.class, "grad_login");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GradLogin.class);
    }

	 /**
	  * 登录操作
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "student-login")
	 @ApiOperation(value="student-login", notes="login")
	 @PostMapping(value = "/login")
	 public Result<?> login(@RequestParam(name="uid",required=true) String id,
							@RequestParam(name="uname",required=true) String name) {
//		 Student student = studentService.getById(id);
		 GradLogin gradLogin = gradLoginService.getById(id);
		 if (gradLogin != null) {
			 if (name.equals(gradLogin.getStuName())) {
				 return Result.OK(gradLogin);
			 }
		 }
		 return Result.error("学号或者姓名错误");
	 }
	 /**
	  *添加收藏
	  * @param
	  * @return
	  */
	 @AutoLog(value = "添加收藏")
	 @ApiOperation(value="添加收藏", notes="添加收藏-添加收藏")
	 @PostMapping(value = "/postCollect")
	 public Result<?> postCollect(@RequestParam(name="collects",required=true) String collects,
								  @RequestParam(name="uid",required=true) String id)
	 {
		 GradLogin gradlogin = gradLoginService.getById(id);
		 if(gradlogin.getCollect()==null||gradlogin.getCollect().equals("")){
		 	gradlogin.setCollect(collects);
	 	 }else{
			 String s=gradlogin.getCollect()+','+collects;
			 gradlogin.setCollect(s);
		 }
		 gradLoginService.updateById(gradlogin);
		 return Result.OK("添加成功!");
//		 log.info(gradlogin.getCollect());
	 }

	 /**
	  *取消收藏
	  * @param
	  * @return
	  */
	 @AutoLog(value = "取消收藏")
	 @ApiOperation(value="取消收藏", notes="取消收藏-取消收藏")
	 @PostMapping(value = "/deleteCollect")
	 public Result<?> deleteCollect(@RequestParam(name="collects",required=true) String collects,
								  @RequestParam(name="uid",required=true) String id)
	 {
		 GradLogin gradlogin = gradLoginService.getById(id);
		 String s=gradlogin.getCollect();
		 if(s.length()==10)
		 {
			 s=s.replaceAll(collects,"");
		 }
		 else
		 {
			 if(s.indexOf(collects)==0)
			 {
				 s=s.replaceAll(collects+",","");
			 }
			 else
			 {
			 	 s=s.replaceAll(","+collects,"");
			 }
		 }
		 gradlogin.setCollect(s);
		 gradLoginService.updateById(gradlogin);
		 return Result.OK("删除成功!");
//		 log.info(gradlogin.getCollect());
	 }
	 /**
	  *查询收藏
	  * @param
	  * @return
	  */
	 @AutoLog(value = "查询收藏")
	 @ApiOperation(value="查询收藏", notes="查询收藏-查询收藏")
	 @PostMapping(value = "/selectCollect")
	 public Result<?> selectCollect(@RequestParam(name="collects",required=true) String collects,
									@RequestParam(name="uid",required=true) String id)
	 {
		 GradLogin gradlogin = gradLoginService.getById(id);
		 String s=gradlogin.getCollect();
		 Boolean b=false;
		 if(gradlogin.getCollect()==null||gradlogin.getCollect().equals(""))
		 {
		 	b=false;
		 }
		 if(s.indexOf(collects)!=-1) {
		 	 b=true;
		 }
		 else {
		     b=false;
		 }
		 return Result.OK(b);
	 }
	 /**
	  *查询学生
	  * @param
	  * @return
	  */
	 @AutoLog(value = "查询学生")
	 @ApiOperation(value="查询学生", notes="查询学生-查询学生")
	 @PostMapping(value = "/selectStudentlist")
	 public Result<?> selectStudentlist(@RequestParam(name="uid",required=true) String id)
	 {
		 GradLogin gradlogin = gradLoginService.getById(id);
		 String s=gradlogin.getCollect();
		 String lists[]=s.split(",");


		 ArrayList<Student> list=new ArrayList<Student>();
		 for(int i=0;i<lists.length;i++)
		 {
			 Student student = studentService.getById(lists[i]);
			 list.add(student);
		 }

		 return Result.OK(list);
	 }
 }
